package cn.gwm.flink.streaming.dwm.charge.template;

import cn.gwm.flink.streaming.config.ChargeConfigEnum;
import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.dwm.charge.bean.ChargeArgs;
import cn.gwm.flink.streaming.dwm.charge.support.ChargeFieldSupport;
import cn.gwm.flink.streaming.dwm.charge.support.ChargeStateSupport;
import cn.hutool.json.JSONObject;

/**
 * @ClassName ChargingChargeState
 * @Description 充电中
 * @Author LiangGuang
 * @Date 2023/05/11 18:04
 */
public class ChargingChargeState extends ChargeStateModel {
    @Override
    protected void stateStartSelfHandle(ChargeArgs args) throws Exception {
        // 判断逻辑充电次数 是否 +1
        JSONObject summar = args.getSummarState().value();
        JSONObject bandSummar = args.getBandSummarState().value();
        if (summar.getInt("chrgLogicNum") == 0) {
            summar.set("chrgLogicNum", 1);
        } else {
            Long breakTime = args.getChargeBreakState().value();
            if (breakTime != null && (Math.abs(args.getFirstData().getLong(BaseFields.tid) - breakTime) > Long.valueOf(ChargeConfigEnum.CHARGEINTERRUPT.getValue()) * 60 * 1000L)) {
                summar.set("chrgLogicNum", summar.getInt("chrgLogicNum") + 1);
            }
        }
        bandSummar.set("chrgLogicNum", summar.get("chrgLogicNum"));

        // 对有效充电时长进行赋0
        if (args.getEffectiveChargeTime().value() == null) {
            args.getEffectiveChargeTime().update(0L);
        }
        // 物理充电中 首次 进入 充电中 状态，则赋值 汇总开始充电字段 fixme: ===>
//        ChargeFieldSupport.placeMatching(summar, args);
        if (!summar.containsKey("startChargeTime")) {
            // 赋值 字段
            ChargeStateSupport.replenishStart(args.getConstant(), summar, args.getFirstData());
            summar.set(BaseFields.tid, summar.getLong("chrgConnectTid"));
            ChargeFieldSupport.setChargeType(args.getConstant(), args.getRealNowData(), summar);
            ChargeFieldSupport.setChargeModel(args.getConstant(), args.getRealNowData(), summar);
            bandSummar.set("chargeType", summar.get("chargeType"));
            bandSummar.set("aoiId", summar.get("aoiId"));
            bandSummar.set("adCode", summar.get("adCode"));
            bandSummar.set("aoiType", summar.get("aoiType"));
            bandSummar.set("poiId", summar.get("poiId"));
            bandSummar.set("poiType", summar.get("poiType"));
            if (summar.containsKey("chargeModel")) {
                bandSummar.set("chargeModel", summar.get("chargeModel"));
            }
            // 如果查抢时间和开始充电时间一样，则不完整
            if (summar.getLong("chrgConnectTid").equals(summar.getLong("startChargeTime"))) {
                summar.set("complete", 0);
                bandSummar.set("complete", 0);
            }
        }
        args.getChargeBreakState().clear();
        args.getSummarState().update(summar);
        args.getBandSummarState().update(bandSummar);
        collector(args);
    }

    @Override
    protected void stateEndSelfHandle(ChargeArgs args) throws Exception {
        // 结束的时候，复制结束字段到 汇总
        JSONObject summar = args.getSummarState().value();
        ChargeStateSupport.replenishEnd(args.getConstant(), summar, args.getPrevEndData());

        // 更新 有效充电时长
        JSONObject bandSummar = args.getBandSummarState().value();
        Long chargeTime = bandSummar.getLong("endChargeTime") - bandSummar.getLong("startChargeTime");
        bandSummar.set("effectiveChargeTime", chargeTime / 1000L);
        args.getBandSummarState().update(bandSummar);

        Long value = args.getEffectiveChargeTime().value();
        value += chargeTime / 1000L;
        args.getEffectiveChargeTime().update(value);
        summar.set("effectiveChargeTime", value);

        args.getSummarState().update(summar);


    }


    /**
     * 缓存中的明细数据
     *
     * @param args
     * @throws Exception
     */
    public void collector(ChargeArgs args) throws Exception {
        // 将list中的数据
        Iterable<JSONObject> datas = args.getListState().get();
        if (datas != null) {
            datas.forEach(item -> {
                JSONObject detail = null;
                try {
                    detail = ChargeStateSupport.createDetail(args, item);
                    args.getOut().collect(detail);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        args.getListState().clear();
    }

}
